<template>
	<view class="container">
		<image src="../../static/l2.png" mode="aspectFill" class="bg"></image>
		<view class="title">
			<image src="../../static/l1.png" mode="aspectFill"></image>
		</view>
		<view class="list">
			<view class="item">
				<input type="number" v-model="params.phone" placeholder-style="color:#666;font-size: 28rpx;"
					placeholder="请输入手机号" />
			</view>
			<view class="item">
				<input v-model="params.code" type="text" placeholder-style="color:#666;font-size: 28rpx;"
					placeholder="请输图形验证码" />
				<image :src="img" mode="aspectFill" @click="getImg"></image>
			</view>
			<view class="item">
				<input v-model="params.smsCode" type="text" placeholder-style="color:#666;font-size: 28rpx;"
					placeholder="请输入手机验证码" />
				<text :class="{on:canSend}" @click="getcode">{{codeText}}</text>
			</view>
		</view>
		<view class="btn" @click="submit">
			确认
		</view>
	</view>
</template>

<script>
	import {
		getCodeImg,sendphone
	} from '@/common/http.api.js';
	let timer = null
	export default {
		data() {
			return {
				canSend: true,
				codeText: '获取验证码',
				img: '',
				params: {
					phone: '',
					code: '',
					smsCode: '',
					uuid: '',
					openId: '',
				}
			}
		},
		onLoad() {
			this.params.openId = uni.getStorageSync('openId')
			this.getImg()
		},
		methods: {
			getImg() {
				getCodeImg({}).then(res => {
					if (res.code == 200) {
						this.img = `data:image/png;base64, ${res.img}`
						this.params.uuid = res.uuid
					}
				})
			},
			submit() {
				if (!this.params.phone) {
					this.$modal.msg('请输入手机号')
					return
				}
				if (!this.params.code) {
					this.$modal.msg('请输入验证码')
					return
				}
				if (!this.params.smsCode) {
					this.$modal.msg('请输入短信验证码')
					return
				}
				phonedouyin(this.params).then(res => {
					console.log(res);
					if (res.code == 200) {
						uni.showToast({
							title: '绑定成功',
							icon: "none"
						});
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/user/user'
							})
						}, 500)
					}
				})
			},
			getcode() {
				if (!this.canSend) {
					this.$modal.msg('请稍后')
					return
				}
				if (!this.params.phone) {
					this.$modal.msg('请输入手机号')
					return
				}
				if (!this.params.code) {
					this.$modal.msg('请输入验证码')
					return
				}
				sendphone(this.params).then(res => {
					if (res.code == 200) {
						this.$modal.msg('发送成功')
						this.canSend = false;
						let num = 60;
						timer = setInterval(() => {
							num--;
							this.codeText = num + 's后重发';
							if (num == 0) {
								clearInterval(timer);
								this.codeText = '重新发送';
								this.canSend = true
							}
						}, 1000)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
		padding: 90rpx 40rpx;

		.btn {
			position: relative;
			z-index: 999;
			width: 100%;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			background: linear-gradient(270deg, #FF6F07 0%, #FFA666 100%);
			border-radius: 20rpx;
			color: #fff;
			margin-top: 40rpx;
			font-size: 30rpx;

		}

		.bg {
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			width: 100%;
			height: 1040rpx;
			z-index: 1;
		}

		.title {
			width: 380rpx;
			height: 102rpx;
			position: relative;
			z-index: 999;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.list {
			margin-top: 114rpx;
			position: relative;
			z-index: 999;

			.item {
				height: 100rpx;
				border-radius: 12rpx;
				background-color: #fff;
				margin-bottom: 34rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				text-align: center;
				padding: 0 30rpx;

				&:last-child {
					margin-bottom: 0;
				}


				input {
					height: 60rpx;
					line-height: 60rpx;
					color: #333;
					font-size: 28rpx;
					flex: 1;
					width: 1rpx;
					text-align: left;
				}

				image {
					width: 120rpx;
					height: 60rpx;
					margin-left: 20rpx;
				}

				text {
					color: #FF6F07;
					font-size: 28rpx;
					margin-left: 20rpx;
				}
			}
		}
	}
</style>